<template>
	<view>
		<uni-nav-bar fixed left-icon="back" title="优惠券"></uni-nav-bar>
		<view class="navbar">
			<view v-for="(item, index) in navList" :key="index"
			      class="nav-item"
			      :class="{current: tabCurrentIndex === index}"
			      @click="tabClick(index)"
			>
				{{item.text}}
			</view>
		</view>

		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item v-for="(tabItem, tabIndex) in navList" :key="tabIndex">
				<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
					<view v-for="(item, index) in tabItem.orderList" :key="index" class="order-item">
						<coupon v-bind:item="item"
								types="carts"
								@qrcode="qrCode"
								theme="#ff6c00"
								color="#ffffff"
								solid="#ff6c00">
						</coupon>
					</view>
					<view class="tui-tail" v-if="tabItem.orderList.length">
						<text>- 没有更多了-</text>
					</view>
				</scroll-view>
				<empty v-if="!tabItem.orderList.length && !isLoading"></empty>
				<load v-model="isLoading"></load>
			</swiper-item>
		</swiper>

		<view v-if="showModal">
			<view class="modal-mask"></view>
			<view class="modal-dialog">
				<image :src="qrcode" style="width: 500rpx"></image>
				<view class="coupon-money">
					<view class="layof">￥{{form.faceValue}}</view>
					<view class="nick">{{form.name}}</view>
				</view>
				<view style="position: absolute; top: 108%;" @tap="_onClose">
					<uni-icons type="close" size="80" color="#ccc"></uni-icons>
				</view>
			</view>

		</view>

		<!-- 登录提示 -->
		<uni-login-model></uni-login-model>

	</view>
</template>
<script>
    import uniLoginModel from '@/components/uni-login-model/uni-login-model.vue'
    import coupon from '@/components/coolc-coupon/coolc-coupon';
    import uniIcons from '@/components/uni-icons/uni-icons'
    import empty from '@/components/empty/index.vue'
    import load from '@/components/empty/load.vue'

	export default {
		components: {
            uniLoginModel, coupon, uniIcons, empty, load
		},
		data() {
			return {
                tabCurrentIndex: 0,
                navList: [
                    { state: 0, text: '未使用', loadingType: 'more', orderList: [] },
                    { state: 1, text: '已使用', loadingType: 'more', orderList: [] },
                    { state: 2, text: '已过期', loadingType: 'more', orderList: [] }
                ],
                qrcode: '',
                showModal: false,
                top: 0,
                form: {
                    name: '',
                    faceValue: ''
                },
                isLoading: true
			}
		},
		onLoad() {
			this.loadData()
		},
		methods: {
            //	获取优惠券列表
            loadData(source) {
                let index = this.tabCurrentIndex;
                let navItem = this.navList[index];
                let state = navItem.state;
                if(navItem.loadingType === 'loading'){return;}
                navItem.loadingType = 'loading';
                this.isLoading = true
                this.$api.api('coupon.list',{status: state}).then(res => {
                    if (res.code == 200 ){
                        res.data.forEach(item=>{
                            navItem.orderList.push(item);
                        })
                    } else {
                        this.$api.msg(res.message)
                    }
                    this.isLoading = false
                }).catch(error => {
                    this.isLoading = false
                    this.$api.msg(error.message)
                })
            },
            //顶部tab点击
            tabClick(index){
                this.tabCurrentIndex = index;
            },
            //swiper 切换
            changeTab(e){
                this.tabCurrentIndex = e.target.current;
                this.loadData('tabChange');
            },
            qrCode(item) {
                this.form = item
                uni.showLoading({title: ''})
                this.$api.api('coupon.code', {content: item.couponId}).then(res => {
                    if (res.code == 200) {
                        this.qrcode = res.data
                        uni.hideLoading();
                        this.showModal = true
                    } else {
                        uni.hideLoading();
                        uni.showToast({title: res.message, icon: 'none'})
                    }
                }).catch()
            },
            _onClose() {
                this.showModal = false
            }
		}
	}
</script>
<style lang="scss">
	.swiper-box {
		height: calc(100vh - 40px);
		margin-top: 84rpx;
		padding-bottom: 40rpx;
		.list-scroll-content {
			/*height: 100%;*/
			.order-item {
				display: flex;
				background: #fff;
				margin: 40rpx 40rpx;
				border-radius: 15rpx;
			}
		}
	}
	.navbar {
		display: flex;
		background-color: $uni-bg-color;
		height: 80rpx;
		padding: 0 10rpx;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,.06);
		position: fixed;
		width: 100%;
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 30rpx;
			color: $uni-text-color-grey;
			position: relative;
			&.current{
				color: #000000;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 80rpx;
					height: 0;
					border-bottom: 6rpx solid $uni-color-success;
				}
			}
		}
	}
	/* 蒙层 */
	.modal-mask {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: #000;
		opacity: 0.5;
		overflow: hidden;
		z-index: 1000;
		color: #fff;
	}
	/* 弹框 */
	.modal-dialog{
		font-size: 30rpx;
		width: 90%;
		position: fixed;
		top: 25%;
		left:0;
		right: 0;
		margin:auto;
		z-index: 1500;
		background: #ffffff;
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 30rpx;
		.coupon-money {
			margin-top: 10rpx;
			.nick { width:100%; height:50upx; line-height: 1.8; font-size:26rpx; color:#999; }
			.layof { width:100%; height:48upx; line-height: 0.8; font-size:44upx; color:#ff9000; font-weight:bold; }
		}
	}
</style>